package com.arunsawad.ilertu.common;

import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.URL;

import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.database.Cursor;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Matrix;
import android.net.Uri;
import android.provider.MediaStore;

import com.arunsawad.ilertu.R;

public class Utils {
	// get image from url
	// used for async tasks
	public static Bitmap getBitmapFromURL(String src) {
		try {
			URL url = new URL(src);
			HttpURLConnection connection = (HttpURLConnection) url
					.openConnection();
			connection.setDoInput(true);
			connection.connect();
			InputStream input = connection.getInputStream();
			Bitmap myBitmap = BitmapFactory.decodeStream(input);
			return myBitmap;
		} catch (IOException e) {
			e.printStackTrace();
			return null;
		}
	}

	// alert - pop up message
	public static void alert(Context context, String message) {
		AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(
				context);
		alertDialogBuilder
				.setMessage(message)
				.setCancelable(false)
				.setPositiveButton(R.string.ok,
						new DialogInterface.OnClickListener() {
							public void onClick(DialogInterface dialog, int id) {
								dialog.cancel();
							}
						});
		AlertDialog alert = alertDialogBuilder.create();
		alert.show();
	}

	public static String getPathFromURI(Context context, Uri uri) {
		String path = null;
		String[] proj = { MediaStore.Images.Media.DATA };
		Cursor cursor = context.getContentResolver().query(uri, proj, null,
				null, null);
		if (cursor != null) {
			if (cursor.moveToFirst()) {
				int column_index = cursor
						.getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
				path = cursor.getString(column_index);
			}
			cursor.close();
		}
		return path;
	}

	public static int getImageAngle(Context context, Uri uri) {
		int orientation = 0;
		final String[] proj = new String[] { MediaStore.Images.Media.ORIENTATION };
		final Cursor cursor = context.getContentResolver().query(uri, proj,
				null, null, null);
		if (cursor != null) {
			final int orientationColumnIndex = cursor
					.getColumnIndex(MediaStore.Images.Media.ORIENTATION);
			if (cursor.moveToFirst()) {
				orientation = cursor.isNull(orientationColumnIndex) ? 0
						: cursor.getInt(orientationColumnIndex);
			}
			cursor.close();
		}
		return orientation;
	}

	public static Bitmap getImageFromPath(Context context, String path,
			int orientation) {
		File imgFile = new File(path);
		Bitmap photo = BitmapFactory.decodeFile(imgFile.getAbsolutePath());

		int width = photo.getWidth();
		int height = photo.getHeight();

		// createa matrix for the manipulation
		Matrix matrix = new Matrix();
		// resize the bit map
		// matrix.postScale(scaleWidth, scaleHeight);
		// rotate the Bitmap
		matrix.postRotate(orientation);

		// recreate the new Bitmap
		Bitmap resizedBitmap = Bitmap.createBitmap(photo, 0, 0, width, height,
				matrix, true);

		return resizedBitmap;
	}

	public static Bitmap resizeImage(Bitmap photo, int maxWidth, int maxHeight) {
		int width = photo.getWidth();
		int height = photo.getHeight();
		int newWidth;
		int newHeight;

		if (height < maxHeight && width < maxWidth) {
			newHeight = height;
			newWidth = width;
		} else {
			float scale = ((float) width) / height;
			if (scale < 1) {
				newHeight = maxHeight;
				newWidth = (int) (scale * newHeight);
			} else {
				newWidth = maxWidth;
				newHeight = (int) (newWidth / scale);
			}
		}
		return Bitmap.createScaledBitmap(photo, newWidth, newHeight, false);
	}
}
